package com.zpp.wedding_admin.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zpp.wedding_admin.entity.Banner;
import com.zpp.wedding_admin.entity.Camerist;
import com.zpp.wedding_admin.entity.User;
import com.zpp.wedding_admin.service.ICameristService;
import com.zpp.wedding_admin.service.IUserService;
import com.zpp.wedding_admin.utils.LQWUtils;
import com.zpp.wedding_common.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 朱盼盼
 * @since 2022-12-29
 */
@Api(description = "摄影师管理")
@CrossOrigin
@RestController
@RequestMapping("/weddingadmin/camerist")
public class CameristController {

    @Autowired
    private ICameristService cameristService;

    @Autowired
    private IUserService userService;

    @ApiOperation(value = "分页获取所有摄影师")
    @GetMapping("getCameristPageVo/{currentPage}/{size}")
    public R getBannerPageVo(@PathVariable Long currentPage,
                             @PathVariable Long size) {

        Page<Camerist> page = new Page<Camerist>(currentPage, size);
        cameristService.page(page, null);
        List<Camerist> cameristList = page.getRecords();
        long total = page.getTotal();
        return R.ok().data("cameristList", cameristList).data("total", total);
    }

    @ApiOperation(value = "更新或申请摄影师")
    @PostMapping("applyCamerist")
    public R applyCamerist(@RequestBody Camerist camerist) {
        boolean b;
        camerist.setIsPass(2);
        if ( camerist.getCameristId() == null ) {
            b = cameristService.save(camerist);
        } else {
            b = cameristService.updateById(camerist);
        }
        if ( b ) return R.ok();
        return R.error();
    }

    @ApiOperation(value = "通过摄影师")
    @GetMapping("pass/{id}/{userId}")
    public R pass(@PathVariable Integer id, @PathVariable Integer userId) {
        Camerist camerist = new Camerist();
        camerist.setCameristId(id).setIsPass(1);
        User user = new User();
        user.setUserId(userId).setIsCamera(1);
        boolean b = cameristService.updateById(camerist);
        boolean a = userService.updateById(user);
        if ( a && b ) return R.ok();
        return R.error();
    }

    @ApiOperation(value = "不通过")
    @GetMapping("noPass/{id}/{reason}")
    public R noPass(@PathVariable Integer id, @PathVariable String reason) {
        Camerist camerist = new Camerist();
        camerist.setCameristId(id).setIsPass(0).setReason(reason);
        boolean b = cameristService.updateById(camerist);
        if ( b ) return R.ok();
        return R.error();
    }

    @ApiOperation(value = "通过id获取申请记录")
    @GetMapping("getApplyById/{id}")
    public R getApplyById(@PathVariable Integer id) {
        Camerist camerist = cameristService.getById(id);
        return R.ok().data("camerist", camerist);
    }

    @ApiOperation(value = "通过id获取申请记录")
    @GetMapping("getApplyByUserId/{userId}")
    public R getApplyByUserId(@PathVariable Integer userId) {
        LambdaQueryWrapper<Camerist> lqw = LQWUtils.getLqw(Camerist.class);
        lqw.eq(Camerist::getUserId, userId);
        Camerist camerist = cameristService.getOne(lqw);
        if ( camerist != null ) {
            return R.ok().data("camerist", camerist).data("have", true);
        } else {
            return R.ok().data("have", false);
        }
    }

}
